package com.hspedu.com.jdbc;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author DL5O
 * @version 1.0
 */
public class JdbcConn {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver =(Driver)clazz.newInstance();

        String url = "jdbc:mysql://localhost:3306/ywl_db02";

    }

    @Test
    //方式一:使用传统方法直接new，这是静态加载的
    public void connect01() throws SQLException {
        Driver driver = new com.mysql.jdbc.Driver();
        String url = "jdbc:mysql://localhost:3306/ywl_db02";

        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

    //方式二:使用反射加载Driver类，这是动态加载的
    @Test
    public void connect02() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //使用反射加载Driver类
        Class<?> cls = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)cls.newInstance();

        String url = "jdbc:mysql://localhost:3306/ywl_db02";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");

        Connection connect = driver.connect(url, properties);
        System.out.println("连接方式二:"+connect);
    }

    //方式3 使用DriverManager 替代Diver进行统一连接
    @Test
    public void connect03() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //使用反射加载Driver
        Class<?> cls = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)cls.newInstance();

        //创建url 和 user，password
        String url = "jdbc:mysql://localhost:3306/ywl_db02";
        String user = "root";
        String password = "123456";

        DriverManager.registerDriver(driver);//注册Driver驱动
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("连接方式三:" + connection );
    }

    //使用反射来完成自动注册，简化代码,使用Class.forName 自动完成注册驱动
    @Test
    public void connect04() throws ClassNotFoundException, SQLException {
        //使用反射加载了 Driver类
        //
        //Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/ywl_db02";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println("连接方式四:"+conn);
    }

    //方式五，在方式4的基础上改进，增加配置文件，让连接mysql更加灵活
    @Test
    public void connect05() throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        String filePath = "src\\jdbc.properties";
        properties.load(new FileReader(filePath));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        Class<?> cls = Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println("连接方式5:" + connection);
    }

}
